package com.lbn.jfqsc.widget;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;


import com.lbn.baseframe.utils.SDCardUtils;
import com.lbn.jfqsc.R;

import java.io.File;
import java.util.Date;

/**
 * Created by Administrator on 2016/11/24.
 */

public class PhotoDialog {


    public static Uri makeSelectPhoto(final Activity context){

        final BottomSheetDialog dialog = new BottomSheetDialog(context);
        dialog.setContentView(R.layout.dialog_select_photo);
        dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);
        //相册
        dialog.findViewById(R.id.photo_album).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();

                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                context.startActivityForResult(intent, 1);
            }
        });
        //照相机
        ContentValues contentValues = new ContentValues(1);
        contentValues.put(MediaStore.Images.Media.DATA, new File(SDCardUtils.getRootDirectoryPath()+File.separator+new Date().getTime()+".jpg").getAbsolutePath());
        final Uri imageUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
        dialog.findViewById(R.id.camera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();

                // 判断存储卡是否可以用，可用进行存储
                Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//                if (FileUtils.hasSdcard()) {
                intentFromCapture.putExtra("return-data", false);
                intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                intentFromCapture.putExtra("noFaceDetection", true);
//                }
//                Intent intentFromCapture = null;
//                PackageManager packageManager = context.getPackageManager();
//                Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//                List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
//                for (ResolveInfo res : listCam) {
//                    intentFromCapture = new Intent(captureIntent);
//                    intentFromCapture.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
//                    intentFromCapture.setPackage(res.activityInfo.packageName);
//                    if (imageUri != null) {
//                        intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//                    }
//                }

                context.startActivityForResult(intentFromCapture, 2);
            }
        });
        //取消
        dialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return imageUri;
    }

}
